<template>
  <div class="app-container org">
    <el-card class="leftCard">
      <!-- 搜索条件盒子 -->
      <div>
        <p class="pinformation">搜索条件</p>
        <el-form :model="queryParams" ref="queryRef" @submit.native.prevent>
          <el-row>
            <el-col :span="15">
              <el-form-item label="方案编号：" prop="projectCode">
                <el-input v-model="queryParams.projectCode" placeholder="请输入方案编号" maxlength="40" clearable
                  @keyup.enter="handleQuery" />
              </el-form-item>
            </el-col>
            <el-col :span="8" :offset="1">
              <el-row justify="end">
                <el-button type="success" @click="handleQuery">搜索</el-button>
                <el-button class="business resetBtn" @click="resetQuery">重置</el-button>
              </el-row>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div>
        <el-row>
          <p class="pinformation">有组织废气列表</p>
          <p class="totalStyle">共 {{ total }} 条数据</p>
          <el-row style="padding-bottom: 5px;margin-left: 15px;">
          </el-row>
        </el-row>

        <el-table ref="mainTableRef" class="business" v-loading="loading" :data="projectList"
          :row-class-name="tableRowClass" @row-click="rowClick">
          <el-table-column label="方案编号" align="center" prop="projectCode" />
          <el-table-column label="方案名称" align="center" prop="projectName" />
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </div>
    </el-card>

    <el-card class="rightCard">
      <div v-if="formData.tFId" :id="'dataFormMainBox' + formData.tFId">
        <comMy :params="formData" :tFId="formData.tFId" @returnFormulaList="getFormulaList" ref="tableRef"/>
      </div>
      <template #footer>
        <el-row style="justify-content: end">
          <el-form-item label="公式列表：" prop="formula" label-width="80" v-if="formulaList.length">
            <el-select v-model="formula" style="width: 500px">
              <el-option v-for="item in formulaList" :key="item.formulaId" :value="item.formulaId"
                :label="item.formulaExpressDisplay">
                <el-tooltip class="box-item" effect="customized" placement="top" :content="item.formulaExpressDisplay">
                  <div style="
                        white-space: nowrap;
                        overflow: hidden;
                        text-overflow: ellipsis;
                      ">
                    {{ item.formulaExpressDisplay }}
                  </div>
                </el-tooltip>
              </el-option>
            </el-select>
          </el-form-item>
          <el-button style="background-color: #0066ff; color: #fff; margin-left: 15px" @click="eventResult" v-if="formulaList.length">计算结果</el-button>
          <el-button type="success" @click="handleRefresh">刷新</el-button>
          <el-button type="success" @click="printDateForm">打印</el-button>
          <el-button type="success" @click="exportDateFormPdf">导出pdf</el-button>
          <el-button type="success" @click="saveData">保 存</el-button>
        </el-row>
      </template>
    </el-card>
  </div>
</template>

<script setup name="ResultWrite">
import { resultWriteList } from "@/api/lab/resultWrite";

const { proxy } = getCurrentInstance();
import comMy from "@/views/cyCheck/public/common/comMy";
import bus from "@/utils/eventBus";
const loading = ref(true);
// const loadingBtn = ref(false)
const total = ref(0);
const projectList = ref([]);
const heightBright = ref("");
const formData = ref({});
const formulaList = ref([]);
const formula = ref();
const tableRef = ref(null);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectCode: null,
    projetName: null,
  },
});

const { queryParams, form } = toRefs(data);

/** 查询计量单位库列表 */
function getList() {
  loading.value = true;
  resultWriteList(queryParams.value).then((response) => {
    projectList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

const myId = ref("")
const taskId = ref("")
/** 修改按钮操作 */
function rowClick(row) {
  formData.value.tFId = ""
  nextTick(() => {
    heightBright.value = row.myId
    formData.value = JSON.parse(JSON.stringify(row))
    myId.value = row.myId
    taskId.value = row.taskId
    formData.value.userStatus = 0
    formData.value.userStepId = 0
  })
}
function getFormulaList(list) {
  if (list.length > 0) {
    formulaList.value = list;
    formula.value = list[0].formulaId;
  }
}

function eventResult(){
  tableRef.value.eventResult();
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function tableRowClass({ row }) {
  if (row) {
    if (row.myId == heightBright.value) {
      return "current-row";
    }
    return "";
  }
}

function saveData() {
  bus.emit("saveData");
}


getList();

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [
        { tableName: "main", bottomHeight: 74 },
      ];
    } else {
      heightParams.value = [
        { tableName: "main", bottomHeight: 21 },
      ];
    }
    getTableHeight();
  }
);

const heightParams = ref([
  { tableName: "main", bottomHeight: 21 },
]);
function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}

onMounted(() => {
  nextTick(function () {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
})

onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

function printDateForm() {
  bus.emit("printDateForm");
}

function exportDateFormPdf() {
  proxy.download("/lims/tablePdf/pcViewPdf/" + taskId.value + "/" + myId.value, {}, `有组织废气结果计算表.pdf`);
}

function handleRefresh() {
  bus.emit("showTable")
}

</script>